home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / newsgrp / group93a.txt / 000104_icon-group-sender _Fri Apr 2 07:56:52 1993.msg < prev    next >
Internet Message Format  |  1993-04-21  |  3KB

  1. Received: by cheltenham.cs.arizona.edu; Wed, 7 Apr 1993 12:47:40 MST
  2. Date: 2 Apr 93 07:56:52 GMT
  3. From: ftpbox!mothost!binford!mcdchg!tellab5!ruth.wheaton.edu!gmribeir@uunet.uu.net  (Glauber)
  4. Organization: The Bossa Nova University
  5. Subject: Is this passable code?
  6. Message-Id: <1993Apr2.075652.11967@wheaton.wheaton.edu>
  7. Sender: icon-group-request@cs.arizona.edu
  8. To: icon-group@cs.arizona.edu
  9. Status: R
  10. Errors-To: icon-group-errors@cs.arizona.edu
  11.  
  12.  
  13. My apologies if this is repeated, but this is the 3rd time
  14. that i'm trying to post this. News sometimes behaves weird
  15. here.
  16.  
  17. I started using Icon recently, and am impressed with the
  18. power of the language. But, since i'm learning on my own and
  19. don't have experience with Icon, i don't know if i'm doing
  20. things right. Recently i wrote a short program to find
  21. anagrams (yeah...) using the unix dictionary. I'd appreciate
  22. any comments/suggestions/flames, etc about my programming
  23. style and the Icon way.
  24.  
  25.                     Thank you very much
  26.  
  27.                         Glauber.
  28.  
  29.  
  30. ----------------------------------------------------------------------
  31. # Find all the anagrams of a given word
  32. #    In the Unix dictionary
  33. # Tue Mar 30 07:15:53 CST 1993
  34.  
  35.  
  36.  
  37. procedure main(argv)
  38.  
  39.    word := argv[1]
  40.  
  41.    if not ( in := open("/usr/dict/words") )
  42.       then stop("Can't open system dictionary! :-( ")
  43.  
  44.    #initialize
  45.    l := map( read(in), &ucase, &lcase )
  46.  
  47.    #main loop
  48.    every ( w := lexperm( word ) ) do {
  49.       while ( l << w ) do 
  50.      if not ( l := map( read(in), &ucase, &lcase ) )
  51.         then return
  52.  
  53.       # at this point, either (l == w) or (l >> w)
  54.       if (l == w) then write(l)
  55.    }
  56. end
  57. #I think this is it!
  58.  
  59.  
  60.  
  61.  
  62. # Lexicographical permutation, 
  63. #    Reingold, Combinatorial Algorithms, page 162
  64. # (this is a generator)
  65.  
  66. procedure lexperm( w )
  67.    # initialize
  68.    w := sortword ( map(w, &ucase, &lcase) )
  69.    repeat {
  70.       suspend (w)
  71.       # now permute
  72.       every  i := (*w - 1) to 1 by (-1)  do  {
  73.      if w[i] << w[i+1] then break
  74.       }
  75.       every  j := *w to 1 by (-1)  do  {
  76.      if w[i] << w[j]   then break
  77.       }
  78.       w[i] :=: w[j]
  79.       r := *w
  80.       s := i + 1
  81.       while r >= s  do  {
  82.      w[r] :=: w[s]
  83.      r -:= 1
  84.      s +:= 1
  85.       }
  86.       # end of permutation
  87.       if (j = 1) & (i = 1) then fail
  88.    }
  89. end
  90.  
  91.  
  92.  
  93. # insertion sort for now...
  94. procedure sortword( w )
  95.    size := *w
  96.    # external loop, traverses the word
  97.    every j := 2 to size do {
  98.       hold := w[j]
  99.       # internal loop, "sinks" each letter in place
  100.       i := j - 1
  101.       while i >= 1  do  {
  102.      if hold >>= w[i] then break
  103.      w[i+1] := w[i]
  104.      i -:= 1
  105.       } 
  106.       w[i+1] := hold
  107.    }
  108.    return w
  109. end
  110.  
  111. ----------------------------------------------------------------------
  112.  
  113.  
  114.  
  115.  
  116. -- 
  117. Glauber Ribeiro
  118. glauber@david.wheaton.edu
  119. glauber%david.wheaton.edu@tellab5.tellabs.com
  120. constantly risking absurdity
  121.